package com.chenglu.android.mine.collect;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.R;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BaseActionBarActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.common.utils.Utils;

import com.chenglu.android.common.widget.SlideRecyclerView;
import com.chenglu.android.goodsinfo.infoutils.GoodsInfoUtils;
import com.chenglu.android.mine.bean.CollectBean;
import com.chenglu.android.mine.beans.adapter.BeansRecordAdapter;
import com.chenglu.android.mine.collect.adapter.CollectAdapter;
import com.chenglu.android.mine.collect.mvp.contract.CollectContract;
import com.chenglu.android.mine.collect.mvp.presenter.CollectPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yalantis.ucrop.util.ScreenUtils;
import com.yl.recyclerview.helper.ItemSwipeCallback;
import com.yl.recyclerview.widget.SlideItemView;
import com.yl.recyclerview.wrapper.SwipeToDismissWrapper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: lyj
 * @Created: by Copyright 2023 6 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：我的收藏
 */
public class CollectActivity extends BaseActionBarActivity<CollectPresenter> implements CollectContract.View {
    @BindView(R.id.rv_collect)
    RecyclerView rvCollect;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.ll_loading_data)
    LinearLayout llLoadingData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;

    private CollectAdapter collectAdapter;

    private  int current = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initView() {
        setCenterText("我的收藏");
        mPresenter = new CollectPresenter();
        mPresenter.attachView(this);
        initAdapter();
        initRefreshLayout();
        mPresenter.getGoodsCollect(getToken(),getMapParams());

    }


     private int mPosition;

    private void initAdapter() {

        collectAdapter = new CollectAdapter(mContext,R.layout.item_collect, list);
        rvCollect.setLayoutManager(new LinearLayoutManager(mContext));
        rvCollect.setAdapter(collectAdapter);
//        commentAdapter.notifyDataSetChanged();


        collectAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                switch (view.getId()){
                   case  R.id.tv_delete:
//                       ToastUtils.show(""+position);
                       CollectBean.Data.Records records = collectAdapter.getData().get(position);

                       mPosition = position;
                       if(records.getCollectType()==1){ //商品
                           mPresenter.getGoodsCollectRemove(getToken(), records.getGoodsId());
                       }else if(records.getCollectType()== 2){ //店铺
                           mPresenter.getShopsCollectRemove(getToken(), records.getTenantId());
                       }

                    break;
                    case R.id.ll_slide:
//                        ToastUtils.show("!!!");
//                        collectAdapter.getData().get(position).setSlideView(true);
//                        collectAdapter.notifyItemChanged(position);
//                        GoodsInfoUtils.goodsInfo(mContext,collectAdapter.getData().get(position).getGoodsId(),1);
                        break;
                }
            }
        });


    }


    //刷新
    private void initRefreshLayout() {

        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //id 需要传参过来
                current = 1;
                //需要清空数据再加
                list.clear();
                mPresenter.getGoodsCollect(getToken(), getMapParams());


            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                current += 1;
                mPresenter.getGoodsCollect(getToken(), getMapParams());

            }
        });
    }

    private Map<String, String> getMapParams() {
        Map<String, String> map = new HashMap<>();
//        map.put("shopsTypeId", shopsTypeId +"");
        map.put("current", current + "");
        map.put("size", Constant.SIZE + "");
//        map.put("goodsName","");
//        map.put("lat",amountType);
//        map.put("lng",amountType);

        return map;
    }

    List<CollectBean.Data.Records> list = new ArrayList<>();

    @Override
    public void getGoodsCollect(CollectBean bean) {
         stopRefresh(smartrefreshlayout);

         if(bean.getData().getTotal()>0){
             llLoadingData.setVisibility(View.VISIBLE);
             llLoadingNoData.setVisibility(View.GONE);
             for (int i = 0; i <bean.getData().getRecords().size() ; i++) {
                 list.add(bean.getData().getRecords().get(i));
             }

             if(bean.getData().getTotal() < Constant.SIZE){
                 smartrefreshlayout.setEnableRefresh(false);
             }
             if(current *Constant.SIZE > bean.getData().getTotal()){
                 smartrefreshlayout.setEnableLoadMore(false);
                 View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                 inflate.findViewById(R.id.tv_no_more).setBackgroundColor(getResources().getColor(R.color.color_f6f6f6));
                 collectAdapter.addFooterView(inflate);
             }

         }else {
              llLoadingData.setVisibility(View.GONE);
              llLoadingNoData.setVisibility(View.VISIBLE);
         }

        collectAdapter.notifyDataSetChanged();
    }


    @Override
    public void getGoodsCollectRemove(SmsCodeBean bean) {
        list.remove(mPosition);
        collectAdapter.setNewData(list);
        collectAdapter.notifyDataSetChanged();
        ToastUtils.show(bean.getMsg());
    }


    @Override
    public void getShopsCollectRemove(SmsCodeBean bean) {
        list.remove(mPosition);
        collectAdapter.setNewData(list);
        collectAdapter.notifyDataSetChanged();
        ToastUtils.show(bean.getMsg());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
